package com.bole.controller;


import com.bole.dao.ContentDao;
import com.bole.dto.content.TbContentDto;
import com.bole.pojo.TbContentDo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("content")
@Api(value = "广告管理api")
public class ContentController {

    @Autowired
    private ContentDao contentDao;

    @GetMapping("getContentInfo")
    @ApiOperation(value = "查询全部广告", notes = "查询全部广告")
    public List<TbContentDto> getContentInfo() {
        List<TbContentDo> all = contentDao.findAll();
        ArrayList<TbContentDto> arrayList = new ArrayList<>();
        for (TbContentDo tbContentDo : all) {
            TbContentDto tbContentDto = new TbContentDto();
            BeanUtils.copyProperties(tbContentDo, tbContentDto);
            arrayList.add(tbContentDto);
        }
        return arrayList;
    }

    @Transactional
    @PostMapping("addContentInfo")
    @ApiOperation(value = "增加广告", notes = "增加广告")
    @ApiImplicitParam(name = "content", value = "广告实体类", required = true, paramType = "body", dataTypeClass = TbContentDo.class)
    public void addContentInfo(@RequestBody TbContentDo content) {
        TbContentDo tbContentDo = contentDao.save(content);
        TbContentDto tbContentDto = new TbContentDto();
        BeanUtils.copyProperties(tbContentDo, tbContentDto);
    }

    @PostMapping("toUpdate")
    @ApiOperation(value = "修改回显查询", notes = "修改回显查询")
    @ApiImplicitParam(name = "id", value = "广告id", required = true, dataTypeClass = Long.class)
    public TbContentDto toUpdate(@RequestParam("id") Long id) {
        TbContentDo tbContentDo = contentDao.findById(id).get();
        TbContentDto tbContentDto = new TbContentDto();
        BeanUtils.copyProperties(tbContentDo, tbContentDto);
        return tbContentDto;
    }


    @PostMapping("updateById")
    @ApiOperation(value = "修改广告", notes = "修改广告")
    @ApiImplicitParam(name = "content", value = "广告实体类", required = true, paramType = "body", dataTypeClass = TbContentDo.class)
    public void updateById(@RequestBody TbContentDo content) {
        TbContentDo tbContentDo = contentDao.save(content);
        TbContentDto tbContentDto = new TbContentDto();
        BeanUtils.copyProperties(tbContentDo, tbContentDto);
    }

    @PostMapping("deleteBatch")
    @ApiOperation(value = "删除广告", notes = "删除广告")
    @ApiImplicitParam(name = "idss", value = "广告id集", required = true, dataTypeClass = Long.class)
    public void deleteBatch(@RequestParam("idss") List<Long> idss) {
        contentDao.deleteInBatchById(idss);
    }

    @GetMapping("getContentInfoByCate/{id}")
    @ApiOperation(value = "根据广告类型查询", notes = "根据广告类型查询")
    @ApiImplicitParam(name = "id", value = "广告分类id", required = true, paramType = "path", dataTypeClass = Long.class)
    public List<TbContentDto> getContentInfoByCate(@PathVariable("id") Long id) {
        List<TbContentDo> list = contentDao.findByContentTypeId(id);
        ArrayList<TbContentDto> tbContentDtoArrayList = new ArrayList<>();
        for (TbContentDo tbContentDo : list) {
            TbContentDto tbContentDto = new TbContentDto();
            BeanUtils.copyProperties(tbContentDo, tbContentDto);
            tbContentDtoArrayList.add(tbContentDto);
        }
        return tbContentDtoArrayList;
    }



}
